<?php

namespace App\Services;
use App\Models\Beat;
use App\Models\Collections;
use App\Models\MusicCreation;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class CollectionService
{

    /**
     * @param $request
     * @param $errmsg
     * @return bool
     * @throws \Exception
     * @name  取消收藏
     * @user IHammer
     */
    public static function CollectionDeleteHandle($request,&$errmsg){
        $type = $request->get("type")?:"";
        $id = $request->get("id")?:0;
        $user_id = $request->get("user_id")?:0;
        if(empty($type)||empty($id)){
            return false;
        }
        if(!in_array($type,["beat",'music'])){
            return false;
        }
        if($type=="beat"){
            $res = Beat::getInfo(['id'=>$id,"status"=>1]);
            if(empty($res)){
                $errmsg = "没有找到对应数据！";
                return false;
            }
        }elseif($type=="music"){
            $res = MusicCreation::getInfo(['id'=>$id,"status"=>1]);
            if(empty($res)){
                $errmsg = "没有找到对应数据！";
                return false;
            }
        }
        $info = Collections::getInfo(['user_id'=>$user_id,'object_id'=>$id,'object_type'=>$type]);
        if(empty($info)){
            $errmsg = "没有找到对应数据";
            return false;
        }
        if($type=="beat"&&$res['collection_num']>0){
            Beat::where(['id' => $id])->decrement('collection_num',1);
        }elseif($type=="music"&&$res['collection_num']>0){
            MusicCreation::where(['id' => $id])->decrement('collection_num',1);
        }
        $res_store['user_id'] = $user_id;
        $res_store['object_id'] = $id;
        $res_store['object_type'] = $type;
        Collections::where($res_store)->delete();
        return true;
    }
    /**
     * @param $request
     * @param $errmsg
     * @return bool
     * @name 点击量集中设置
     * @user IHammer
     */
    public static function CollectionCreateHandle($request,&$errmsg){
        $type = $request->get("type")?:"";
        $id = $request->get("id")?:0;
        $user_id = $request->get("user_id")?:0;
        if(empty($type)||empty($id)){
            return false;
        }
        if(!in_array($type,["beat",'music'])){
            return false;
        }
        if($type=="beat"){
            $beat = Beat::getInfo(['id'=>$id,"status"=>1]);
            if(empty($beat)){
                $errmsg = "没有找到对应数据！";
                return false;
            }
        }elseif($type=="music"){
            $beat = MusicCreation::getInfo(['id'=>$id,"status"=>1]);
            if(empty($beat)){
                $errmsg = "没有找到对应数据！";
                return false;
            }
        }
        //判断是否已经收藏
        $info = Collections::getInfo(['user_id'=>$user_id,'object_id'=>$id,'object_type'=>$type]);
        if($info){
            $errmsg = "已收藏";
            return false;
        }
        if($type=="beat"){
            Beat::where(['id' => $id])->increment('collection_num',1);
        }elseif($type=="music"){
            MusicCreation::where(['id' => $id])->increment('collection_num',1);
        }
        $res_store['user_id'] = $user_id;
        $res_store['object_id'] = $id;
        $res_store['object_type'] = $type;
        Collections::create($res_store);
        return true;
    }

    /**
     * @param $object_id
     * @param $object_type
     * @return int
     */
    public static function isCollection($object_id,$object_type){
        return Collections::where('object_id',$object_id)->where('object_type',$object_type)->where('user_id',_USER_ID)->count();
    }

    /**
     * @param $page
     * @param $pageSize
     */
    public static function CollectionListsHandle($page,$pageSize,$type){
        $query = Collections::whereHas($type,function($query){
        })->with([$type=>function($query) use ($type){
            if($type == 'music'){
                $query->with(['user'=>function($q){
                    $q->select('id','nickname','avatarurl');
                }]);
            }
//            $query->select('beat_title','');
        }])->where('user_id',_USER_ID)->where('object_type',$type);
        $total = $query->count();
        $total_page = ceil($total/$pageSize);
        $lists = $query->orderBy('created_at','desc')->skip(($page-1)*$pageSize)->take($pageSize)->get();
        $newLists = [];
        foreach($lists as $value){
            if($type == 'music'){
                $newLists[] = $value[$type];
            }else{
                $value['beat']['beat_id'] = $value['beat']['id'];
                $value['beat']['beat_url'] = str_replace('https://file.ihammer.cn','',$value['beat']['beat_url']);
                $value['beat']['beat_time'] = $value['beat']['beat_time']?SystemService::changeTimeType($value['beat']['beat_time']):"未读取";
                unset($value['beat']['id']);
                $newLists[] = $value['beat'];
            }

        }
        return ['total'=>$total,'total_page'=>$total_page,'current_page'=>$page,'per_page'=>$pageSize,'data'=>$newLists];
    }
}
